<template>
    <h3>账号密码登录</h3>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
        <p><span>验证码登录</span>|忘记密码？</p>
    </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ref } from 'vue';
import { showNotify } from 'vant';
let router = new useRouter()
const username = ref('');
const password = ref('');
const onSubmit = async (values) => {
    const res = await axios.post('/api/login', values)
    console.log('res', res.data);
    const { code, data, message } = res.data
    if (code === 10000) {
        showNotify({ type: 'success', message: '登录成功' });
        localStorage.setItem('token',JSON.stringify(data))
        router.push('/home')
    } else {
        showNotify({ type: 'danger', message: '登录失败' });
    }
};
</script>

<style>
h3 {
    color: rgb(28, 103, 224);
    margin-left: 30px;
    margin-top: 50px;
}

.van-field {
    margin-bottom: 20px;
}

p {
    text-align: center;
    font-size: 12px;
}

span {
    color: rgb(28, 103, 224);
}
</style>